<?php
class SafeAction extends CommonAction {
	//修改个人信息，显示信息页面
	public function index(){
		
		$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		$this->info = M('Member') -> where($data) -> find();
		$this->display();
	}
	
	
	//设置个人密码
	public function pwdchange(){
		//验证旧密码
		$data['password'] = md5($this->_post('oldpassword'));
		$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		if(!$data['id']){
			$this->error('关联用户不存在！');
		}
		$member = M('Member') -> where($data) -> find();
		if($member){
			//如果这个用户存在了，并且密码正确，验证他的两个新密码是否为空，是否一致
			if($this->_post('newpassword') == ''){
				$this->error('新密码不能为空!');
			}
			if($this->_post('newpassword') != $this->_post('renewpassword')){
				$this->error('两次输入的密码不一致!');
			}
			//都对了，让他修改密码
			$update['password'] = md5($this->_post('newpassword'));
			$ret = M('User') -> where(array('id' => $this -> UserInfo)) -> data($update) -> save();
			$ret1 = M('Member') -> where(array('id' => $data['id'])) -> data($update) -> save();
			if($ret !== false && $ret1 !== false){
				$this->success('修改成功，请重新登录！', U('Public/logout'));
			}
		}else{
			$this->error('原密码错误!');
		}
	}
	
	//设置个人密码
	public function changepwd(){
		//验证旧密码
		//$data['password'] = md5($this->_post('oldpassword'));
		//得到一个用户
		$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		if(!$data['id']){
			$this->error('关联用户不存在！');
		}
		$member = M('Member') -> where($data) -> find();  
		if($member){
			//如果这个用户存在了，并且密码正确，验证他的两个新密码是否为空，是否一致
			if($this->_post('newpassword') == ''){
				$this->error('新密码不能为空!');
			}
			if($this->_post('newpassword') != $this->_post('renewpassword')){
				$this->error('两次输入的密码不一致!');
			}
			//都对了，让他修改密码
			$update['repwd'] = md5($this->_post('newpassword'));
			//$ret = M('User') -> where(array('id' => $this -> UserInfo)) -> data($update) -> save();
			$ret1 = M('Member') -> where(array('id' => $data['id'])) -> data($update) -> save();
			if($ret !== false && $ret1 !== false){
				$this->success('修改成功，请重新登录！', U('Public/logout'));
			}
		}else{
			$this->error('原密码错误!');
		}
	}
	
	
	
	//解绑手机号
	public function mobilebind(){ 
		if(session('verifycode'.$_POST['verifytel'])){
			if($_POST['verify'] == session('verifycode'.$_POST['verifytel'])){
				session('verifycode'.$_POST['verifytel'], NULL);
				$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		        $ret = $this->info = M('Member') -> where($data) -> save(array('VerifyPhone' => 1, 'tel' => $_POST['verifytel']));
				if($ret !== false){
					$this->success('绑定成功！');
				}else{
					$this->error(M('Member') -> getError());
				}
			}else{
				$this->error('验证码错误，请重新输入');
			}
		}else{
			$this->error('验证码不存在！');
		}
	}
	//解绑手机号
	public function mobileunbind(){
		if(session('verifycode')){
			if($_POST['verify'] == session('verifycode')){
				session('verifycode', NULL);
				$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		        $ret = $this->info = M('Member') -> where($data) -> save(array('VerifyPhone' => 0, 'tel' => ''));
				if($ret !== false){
					$this->success('解绑成功！');
				}else{
					$this->error(M('Member') -> getError());
				}
			}else{
				$this->error('验证码错误，请重新输入');
			}
		}else{
			$this->error('验证码不存在！');
		}
	}
    //设置二级码时验证手机号
	public function setpwdverifymobile(){
		if(session('verifycode')){
			if($_POST['verifytelpwd'] == session('verifycode')){
				if($ret !== false){
					//验证正确后修改密码
					if($_POST['pwd'] != $_POST['repwd']){
						$this->error('两次输入的密码不一致');
					}
					if($_POST['pwd'] == ''){
						$this -> error('密码不能为空');
					}
					$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
					$ret = M('Member') -> where($data) -> data(array('repwd' => md5($_POST['pwd']))) -> save();
					if($ret !== false){
						session('verifycode', NULL);
						$this->success('修改成功！');
					}else{
						$this->error(M('Member') -> getError());
					}
					$this->success('修改成功！');
				}else{
					$this->error(M('Member') -> getError());
				}
			}else{
				$this->error('验证码错误，请重新输入');
			}
		}else{
			$this->error('验证码不存在！');
		}
	}
	
	
	
	
	
	//发送验证码
	public function sendmsm(){
		$data['id'] = M('User') -> where(array('id' => $this -> UserInfo)) -> getField('member_id');
		$info = M('Member') -> where($data) -> find();
		if($info['VerifyPhone'] == 0){
			if(isset($_POST['tel'])){
				$info['tel'] = $_POST['tel'];
			}else{
				$this->error('您还没有绑定手机号码');
			}
		}
		define('__RELATEID__', $this->siteid);
		$verifycode = rand(123456,999999);
		session('verifycode', $verifycode);
		if(isset($_POST['tel'])){
			session('verifycode'.$info['tel'], $verifycode);
		}
		$ret = SMSSend($info['tel'], $verifycode.'　,5');
		if($ret){
			$this->success('验证码已经发送，请注意查收');
		}else{
			$this->error('发送验证码失败！');
		}
	}
}